package com.shiyu.exam.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.shiyu.exam.common.Result;
import com.shiyu.exam.entity.Banner;
import com.shiyu.exam.service.BannerService;
import com.shiyu.exam.service.FileUploadService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 轮播图控制器 - 处理轮播图管理相关的HTTP请求
 * 包括图片上传、轮播图的CRUD操作、状态切换等功能
 */
@RestController  // REST控制器，返回JSON数据
@RequestMapping("/api/banners")  // 轮播图API路径前缀
@CrossOrigin  // 允许跨域访问
@Tag(name = "轮播图管理", description = "轮播图相关操作，包括图片上传、轮播图增删改查、状态管理等功能")  // Swagger API分组
public class BannerController {

    @Autowired
    private BannerService bannerService;

    @Autowired
    private FileUploadService fileUploadService;

    @Operation(summary = "获取所有的轮播图信息")
    @GetMapping("/list")
    public Result<List<Banner>> list() {
        //调用BannerService中的list方法
        LambdaQueryWrapper<Banner> bannerLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //排序
        bannerLambdaQueryWrapper.orderByAsc(Banner::getSortOrder);
        //返回结果
        List<Banner> list = bannerService.list(bannerLambdaQueryWrapper);
        return Result.success(list);
    }

    @Operation(summary = "获取激活状态的轮播图信息")
    @GetMapping("/active")
    public Result activeList() {
        //调用BannerService中的list方法
        LambdaQueryWrapper<Banner> bannerLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 查询条件
        bannerLambdaQueryWrapper.eq(Banner::getIsActive, true);
        // 排序
        List<Banner> list = bannerService.list(bannerLambdaQueryWrapper);
        //返回结果
        return Result.success(list);
    }

    @Operation(summary = "根据id获取轮播图信息")
    @GetMapping("/{id}")
    public Result<Banner> getById(@PathVariable Long id) {
        //调用BannerService中的getById方法
        Banner banner = bannerService.getById(id);
        //返回结果
        return Result.success(banner);
    }

//    @Operation(summary = "添加轮播图信息")
//    @PostMapping("/add")
//    public Result add(@RequestBody Banner banner) {
//        //调用BannerService中的save方法
//        boolean save = bannerService.save(banner);
//        //返回结果
//        return save ? Result.success("添加成功") : Result.error("添加失败");
//    }

//    @Operation(summary = "更新轮播图信息")
//    @PutMapping("/update")
//    public Result update(@RequestBody Banner banner) {
//        //调用BannerService中的updateById方法
//        boolean update = bannerService.updateById(banner);
//        //返回结果
//        return update ? Result.success("更新成功") : Result.error("更新失败");
//    }
//
    @Operation(summary = "根据id删除轮播图信息")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        //调用BannerService中的removeById方法
        boolean b = bannerService.removeById(id);
        //返回结果
        return b ? Result.success("删除成功") : Result.error("删除失败");
    }

    @Operation(summary = "添加或更新轮播图信息")
    @RequestMapping(value = {"/add","/update"},method = {RequestMethod.POST,RequestMethod.PUT})
    public Result saveOrUpdate(@RequestBody Banner banner){
        //调用BannerService中的saveOrUpdate方法
        boolean b = bannerService.saveOrUpdate(banner);
        return b ? Result.success("保存成功") : Result.error("保存失败");
    }

    @Operation(summary = "上传轮播图")
    @PostMapping("/upload-image")
    public Result uploadImage(MultipartFile file) {
        String url = fileUploadService.uploadFile(file);
        return Result.success(url, "上传成功");
    }


    @Operation(summary = "切换轮播图状态")
    @PutMapping("/toggle/{id}")
    public Result switchStatus(@PathVariable Long id, @RequestParam Boolean isActive) {
        //调用BannerService中的update方法
        LambdaUpdateWrapper<Banner> bannerLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        // 设置要更新的字段
        bannerLambdaUpdateWrapper.eq(Banner::getId, id);
        bannerLambdaUpdateWrapper.set(Banner::getIsActive, isActive);
        // 执行更新
        boolean update = bannerService.update(bannerLambdaUpdateWrapper);
        return update ? Result.success("切换成功") : Result.error("切换失败");
    }
} 